#include "Order.h"
#include "SystemManager.h"
#include <sstream>
#include <random>
#include <ctime>

// 构造与析构
Order::Order()
    : user(nullptr), flight(nullptr), seat(nullptr),
      totalPrice(0.0), status(OrderStatus::PENDING) {}

Order::~Order() {
    user = nullptr;
    flight = nullptr;
    seat = nullptr;
}

string Order::getOrderId() const {
    return orderId;
}

User* Order::getUser() const {
    return user;
}

void Order::setOrderId(const string& id) {
    orderId = id;
}

void Order::setUser(User* u) {
    user = u;
}

// 初始化订单
void Order::initOrder(User* user, Flight* flight, Seat* seat) {
    this->user = user;
    this->flight = flight;
    this->seat = seat;
    this->orderId = generateOrderId();
    this->status = OrderStatus::PENDING;
    this->totalPrice = flight->getPrice(seat->getType());
    this->createTime = Date::getCurrentDate();
}

// 支付订单
bool Order::pay(const string& paymentMethod) {
    if (status != OrderStatus::PENDING) {
        return false;
    }

    bool paymentSuccess = false;

    if (paymentSuccess) {
        this->status = OrderStatus::PAID;
        this->payTime = Date::getCurrentDate();
        this->paymentMethod = paymentMethod;
        flight->lockSeat(seat->getSeatNo());  // 锁定座位
        return true;
    }
    return false;
}

// 取消订单
bool Order::cancel(const string& reason) {
    if (status == OrderStatus::CANCELED || status == OrderStatus::COMPLETED) {
        return false;
    }

    this->status = OrderStatus::CANCELED;
    this->remark = "取消原因：" + reason;
    double refund = calculateRefund();  // 计算退票费
    flight->unlockSeat(seat->getSeatNo());  // 释放座位
    return true;
}

// 生成订单号
string Order::generateOrderId() {
    time_t now = time(0);
    tm* ltm = localtime(&now);
    string base = to_string(1900 + ltm->tm_year) +
                  to_string(1 + ltm->tm_mon) +
                  to_string(ltm->tm_mday) +
                  to_string(ltm->tm_hour) +
                  to_string(ltm->tm_min) +
                  to_string(ltm->tm_sec);

    if (user) {
        string userIdLast4 = user->getId().substr(user->getId().length() - 4);
        int randomNum = rand() % 1000;
        return "ORD" + base + userIdLast4 + to_string(randomNum);
    } else {
        int randomNum = rand() % 10000;
        return "ORD" + base + to_string(randomNum);
    }
}

// 计算退票金额
double Order::calculateRefund() {
    if (!flight) return 0.0;

    int daysBeforeDeparture = flight->getDepartureDate().diffDays(createTime);
    if (daysBeforeDeparture > 15) {
        return totalPrice * 0.9;
    } else if (daysBeforeDeparture > 7) {
        return totalPrice * 0.7;
    } else if (daysBeforeDeparture > 1) {
        return totalPrice * 0.5;
    } else {
        return totalPrice * 0.2;
    }
}

// 检查是否可改签
bool Order::canChangeFlight() const {
    if (status != OrderStatus::PAID) return false;
    if (!flight) return false;
    return flight->getDepartureDate().diffDays(Date::getCurrentDate()) > 1;
}

// 改签航班
Order* Order::changeFlight(Flight* newFlight, Seat* newSeat) {
    if (!canChangeFlight()) return nullptr;

    Order* newOrder = new Order();
    newOrder->initOrder(user, newFlight, newSeat);

    // 计算差价
    double priceDiff = newFlight->getPrice(newSeat->getType()) - totalPrice;
    if (priceDiff > 0) {
        // 补差价
        newOrder->pay(paymentMethod);
    }

    // 原订单标记为改签
    this->remark = "已改签至" + newFlight->getFlightNo();
    this->status = OrderStatus::COMPLETED;
    return newOrder;
}